/***********************************************************************************
 * 文件名： seg_num_stbar.c
 * 版本： 1
 * 说明：
 * 		数字主题状态栏，处理电池事件，显示电量、模式。
 * 修改记录：
 * 	2024/01/10: 初版。 刘言。
***********************************************************************************/
#include "seg_num_stbar.h"
#include "resource.h"
#include "ui_ctrl.h"
#include "components/lcd_drivers/lcd.h"
#include "components/battery_manager/battery_manager.h"
#include "components/umi/umi_static.h"


/////////////////////// 自定义类型




/////////////////////// 内部函数申明

static void UpdateBatteryDis(seg_num_stbar_t *this);
static void UpdateBatNumDis(seg_num_stbar_t *this);

/////////////////////// 事件

// 电池事件（电池管理组件发来的）
static void BMEvent(bm_event_t e, void *param)   // 此事件跟随状态栏
{
    seg_num_stbar_t *this = (seg_num_stbar_t *)param;

    UpdateBatteryDis(this);
}


///////////////////////////////////// 内部其他函数

static void UpdateBatteryDis(seg_num_stbar_t *this)
{
    u8 batt_volume = BM_GetVolume();

    SgPic_setSource(&this->PicBattery, Pic_T0BatBar[(batt_volume + 4) / 10]);

    if(BM_GetDCConnectStatus() == false)    // 未连接充电器
    {
        SgObj_setStatus(&this->PicCharg, SG_ST_HIDE);
    }
    else    // 已连接充电器
    {
        SgPic_setSource(&this->PicCharg, PIC_T0_CHARG_ADDR);
    }

    if(this->Type == 0)
    {
        if(this->AutoSet)SgPic_setSource(&this->PicMode, PIC_T0_SMART_ADDR);
        else SgPic_setSource(&this->PicMode, PIC_T0_POWER_ADDR);
    }

    UpdateBatNumDis(this);
}

static void UpdateBatNumDis(seg_num_stbar_t *this)
{
    if(this->Type == 1)
    {
        u8 num = BM_GetVolume();
        if(num < 100)
        {
            SgObj_setStatus(&this->PicBatNum[0], SG_ST_HIDE);
            SgPic_setSource(&this->PicBatNum[1], Pic_T0MidNum[num / 10]);
            SgPic_setSource(&this->PicBatNum[2], Pic_T0MidNum[num % 10]);
        }
        else
        {
            SgPic_setSource(&this->PicBatNum[0], Pic_T0MidNum[1]);
            SgPic_setSource(&this->PicBatNum[1], Pic_T0MidNum[0]);
            SgPic_setSource(&this->PicBatNum[2], Pic_T0MidNum[0]);
        }
    }
}

/////////////////////// 创建

seg_num_stbar_t * Seg_CreatNumStBar(u8 type)
{
    int i;

    seg_num_stbar_t *this = malloc(sizeof(seg_num_stbar_t));
    memset(this, 0, sizeof(seg_num_stbar_t));

    this->Type = type;

    Umi_Read(0x0202, (u8 *)&this->AutoSet, 1);

    // 电量图标
    SgPic_init(&this->PicBattery);
    SgObj_setPosition(&this->PicBattery, 4, 125);
    SgObj_setSize(&this->PicBattery, PIC_T0_BAT_BAR_0_SIZE_X, PIC_T0_BAT_BAR_0_SIZE_Y);
    
    // 充电图标
    SgPic_init(&this->PicCharg);
    SgObj_setPosition(&this->PicCharg, 37, 141);
    SgObj_setSize(&this->PicCharg, PIC_T0_CHARG_SIZE_X, PIC_T0_CHARG_SIZE_Y);

    // 模式
    SgPic_init(&this->PicMode);
    if(this->Type == 1)
    {
        SgObj_setPosition(&this->PicMode, 18, 74);
    }
    else
    {
        SgObj_setPosition(&this->PicMode, 18, 132);
    }
    SgObj_setSize(&this->PicMode, PIC_T0_SMART_SIZE_X, PIC_T0_SMART_SIZE_Y);
    if(this->AutoSet)SgPic_setSource(&this->PicMode, PIC_T0_SMART_ADDR);
    else SgPic_setSource(&this->PicMode, PIC_T0_POWER_ADDR);
    
    if(this->Type == 1)
    {
        // 电量数字
        for(i = 0; i < 3; i++)
        {
            SgPic_init(&this->PicBatNum[i]);
            SgObj_setPosition(&this->PicBatNum[i], i * 20, 101);
            SgObj_setSize(&this->PicBatNum[i], PIC_T0_MID_NUM_0_SIZE_X, PIC_T0_MID_NUM_0_SIZE_Y);
        }

        // 百分号
        SgPic_init(&this->PicBfh);
        SgObj_setPosition(&this->PicBfh, 61, 117);
        SgObj_setSize(&this->PicBfh, PIC_T0_BFH_SIZE_X, PIC_T0_BFH_SIZE_Y);
        SgPic_setSource(&this->PicBfh, PIC_T0_BFH_ADDR);
    }

    UpdateBatteryDis(this);
    
    BM_SetCb(BMEvent, this);

    return this;
}

/////////////////////// 删除

void Seg_DeleteNumStBar(seg_num_stbar_t *this)
{
    BM_SetCb(NULL, NULL);
    free(this);
}


// 更新事件，100ms调用一次，看作定时器
void Seg_NumStBarUpdate(seg_num_stbar_t *this)
{
    u8 batt_volume = BM_GetVolume();

    if(BM_GetDCConnectStatus() == false)    // 未连接充电器
    {
        if(batt_volume <= 2)
        {
            Ui_SwitchToPage(Page_CreatWarn, (void *)UI_WARN_NO_BATTERY);
        }
    }
}


